﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LockDemo
{
    public class LocalLock : IDistributedLock
    {
        private readonly ConcurrentDictionary<string, byte> lockCounts = new ConcurrentDictionary<string, byte>();

        public Task<bool> TryAcquireLockAsync(string resourceKey, TimeSpan? lockDuration = null)
        {
            byte lockCount = 0;

            if (lockCounts.TryAdd(resourceKey, lockCount))
            {
                lockCounts[resourceKey] = 1;
                return Task.FromResult(true);
            }
            return Task.FromResult(false);
        }

        public Task ReleaseLockAsync(string resourceKey)
        {
            lockCounts.TryRemove(resourceKey, out _);
            return Task.CompletedTask;
        }
    }
}
